﻿using System;
using System.Windows.Data;

namespace TheOliver.Helper
{
    public class DateTimeConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is DateTime)
            {
                var dt = (DateTime)value;

                if (parameter == null)
                {
                    return dt.ToString(culture);
                }
                else
                {
                    return dt.ToString(parameter as string, culture);
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            var strValue = value as string;

            if (strValue != null)
            {
                return DateTime.Parse(strValue, culture);
            }

            return null;
        }

        #endregion
    }
}